package com.android.canvasproject.model.entity;

import java.text.ParseException;
import java.util.Map;
import java.util.Set;

import android.util.Log;

import com.android.canvasproject.enums.CanvasTypeEnum;
import com.android.canvasproject.view.utils.ConstantsAction;

public class Canvas extends GenericEntity {

	private static final long serialVersionUID = -7933283411936394983L;
	
	private Integer id;

	private String title;
	
	private String detail;
	
	private CanvasTypeEnum canvasType;

	private Integer sponsor;

	private Set<Integer> contributors;
	
	public static Canvas entityStructure(Map<String, Object> map) throws InstantiationException {
		Canvas canvas = new Canvas();
		
		try{
			canvas.setId((Integer) map.get("_id"));
			canvas.setTitle((String) map.get("title"));
			canvas.setCanvasType(CanvasTypeEnum.values()[(Integer) map.get("canvas_type")]);
//			canvas.setContributors(contributors)
			canvas.setDetail((String) map.get("detail"));

			canvas.setSponsor((Integer) map.get("id_person_sponsor"));
			canvas.entityStruture(map);

		} catch (ParseException e) {
			Log.e(Canvas.class.getName(), ConstantsAction.Error.PARSE_ERROR, e);
			throw new org.apache.http.ParseException();

		} 

		return canvas;
	}
	
	public static class CanvasDao {
		public static final String TB_CANVAS = "CANVAS";
		
		public static final String[] COLS_CANVAS = new String[] { "_id", "title",
			"detail", "canvas_type", "id_person_sponsor", "id_ics",
			"creation_date", "last_sync", "last_notification",
			"local_creation", "is_synchronized" };
		
		public static final String DB_CREATE_CANVAS = "CREATE TABLE IF NOT EXISTS CANVAS (_id INTEGER PRIMARY KEY, title VARCHAR(60) NOT NULL, detail TEXT NULL, canvas_type INT NOT NULL, id_person_sponsor INTEGER NOT NULL, id_ics BIGINT UNIQUE, creation_date DATETIME DEFAULT CURRENT_TIMESTAMP, last_sync TIMESTAMP NULL, last_notification TIMESTAMP NULL, local_creation INT DEFAULT 0, is_synchronized INT DEFAULT 0, CONSTRAINT fk_canvas_person FOREIGN KEY (id_person_sponsor) REFERENCES PERSON (_id));";
		
		public static final String DB_CREATE_CONTRIBUTORS = "CREATE TABLE IF NOT EXISTS CONTRIBUTORS (id_person INTEGER NOT NULL, id_canvas INTEGER NOT NULL, PRIMARY KEY (id_person, id_canvas), CONSTRAINT fk_contributors_person FOREIGN KEY (id_person) REFERENCES PERSON (_id), CONSTRAINT fk_contributors_canvas FOREIGN KEY (id_canvas) REFERENCES CANVAS (_id));";
	}

	public Canvas() {
		super();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public CanvasTypeEnum getCanvasType() {
		return canvasType;
	}

	public void setCanvasType(CanvasTypeEnum canvasType) {
		this.canvasType = canvasType;
	}

	public Integer getSponsor() {
		return sponsor;
	}

	public void setSponsor(Integer sponsor) {
		this.sponsor = sponsor;
	}

	public Set<Integer> getContributors() {
		return contributors;
	}

	public void setContributors(Set<Integer> contributors) {
		this.contributors = contributors;
	}
}